package com.exam.bean.privilege;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

/**
 * 部门
 */
@Entity
public class Department implements Serializable{

	private static final long serialVersionUID = -5499241925017728982L;
	private Integer id;//编号
	private String name;//部门名称
	private Boolean visible = true;//是否可见
	private Set<Department> childs=new HashSet<Department>();//下级部门
	private Department parent;//上级部门
	private Set<Employee> employees = new HashSet<Employee>();//该部门员工
	
	
	public Department() {}
	
	public Department(Integer id) {
		this.id = id;
	}

	public Department(String name) {
		this.name = name;
	}
	
	
	@Id @GeneratedValue
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	
	@Column(length=20,nullable=false)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	@Column(nullable=false)
	public Boolean getVisible() {
		return visible;
	}
	public void setVisible(Boolean visible) {
		this.visible = visible;
	}
	
	@OneToMany(cascade={CascadeType.REFRESH,CascadeType.REMOVE},mappedBy="parent")
	public Set<Department> getChilds() {
		return childs;
	}
	public void setChilds(Set<Department> childs) {
		this.childs = childs;
	}

	@ManyToOne(cascade=CascadeType.REFRESH)
	@JoinColumn(name="parnetid")
	public Department getParent() {
		return parent;
	}
	public void setParent(Department parent) {
		this.parent = parent;
	}

	@ManyToMany(mappedBy="departments", cascade=CascadeType.REFRESH)
	public Set<Employee> getEmployees() {
		return employees;
	}

	public void setEmployees(Set<Employee> employees) {
		this.employees = employees;
	}
	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Department other = (Department) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
}
